home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ For TASM / USRGUIDE.PAK / COUNT.ASM < prev    next >
Assembly Source File  |  1996-02-21  |  2KB  |  57 lines

  1. ; Turbo Assembler example. Copyright (c) 1993 By Borland International, Inc.
  2.  
  3. ; COUNT.ASM
  4. ; Small model C++-callable assembler function to count the number of
  5. ; lines and characters in a zero-terminated string.
  6. ;
  7. ; Function prototype:
  8. ;       extern unsigned int LineCount(char * near StringToCount,
  9. ;              unsigned int near * CharacterCountPtr);
  10. ; Input:
  11. ;       char near * StringToCount: pointer to the string on which a
  12. ;       line count is to be performed
  13. ;
  14. ;       unsigned int near * CharacterCountPtr: pointer to the
  15. ;                int variable in which the character count is to be stored
  16. ;
  17. ; Usage: bcc callct.cpp count.asm
  18. ;
  19. ; From the Turbo Assembler User's Guide 
  20. ; Ch. 18: Interfacing Turbo Assembler with Borland C++
  21.  
  22. NEWLINE  EQU     0ah                ;the linefeed character is C's
  23.                                     ; newline character
  24.          .MODEL  SMALL
  25.          .CODE
  26.          PUBLIC  _LineCount
  27. _LineCount       PROC
  28.          push    bp 
  29.          mov     bp,sp
  30.          push    si                 ;preserve calling program's register
  31.                                     ; variable, if any
  32.          mov     si,[bp+4]          ;point SI to the string
  33.          sub     cx,cx              ;set character count to 0
  34.          mov     dx,cx              ;set line count to 0 
  35. LineCountLoop:
  36.          lodsb                      ;get the next character
  37.          and     al,al              ;is it null, to end the string?
  38.          jz      EndLineCount       ;yes, we're done
  39.          inc     cx                 ;no, count another character
  40.          cmp     al,NEWLINE         ;is it a newline?
  41.          jnz     LineCountLoop      ;no, check the next character
  42.          inc     dx                 ;yes, count another line
  43.          jmp     LineCountLoop
  44. EndLineCount:
  45.          inc     dx                 ;count the line that ends with the
  46.                                     ; null character
  47.          mov     bx,[bp+6]          ;point to the location at which to
  48.                                     ; return the character count
  49.          mov     [bx],cx            ;set the character count variable
  50.          mov     ax,dx              ;return line count as function value
  51.          pop     si                 ;restore calling program's register
  52.                                     ; variable, if any
  53.          pop     bp
  54.          ret
  55. _LineCount       ENDP
  56.          END
  57.